package edu.regiscollege.cs312.scheduler;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class FirstComeFirstServe extends Scheduler {
	private final List<Job> jobs;
	private final Map<Job, Long> finishTime;

	public FirstComeFirstServe()
	{
		jobs = new LinkedList<Job>();
		finishTime = new HashMap<Job, Long>();
	}
	
	@Override
	public boolean addJob(Job job, long arrivalTime) {
		super.addJob(job, arrivalTime);
		jobs.add(job);
		return true;
	}
	
	@Override
	public Map<Job, Long> getOutput() {
		return finishTime;
	}

	@Override
	public void run(CPU cpu) {
		long currentTime = 0;
		for (Job job : jobs) {
			long elapsed = cpu.execute(job);
			job.setRemainingTime(job.getRemainingTime() - elapsed);
			currentTime += elapsed;
			if (job.getRemainingTime() == 0) {
				finishTime.put(job, currentTime);
			}
			else {
				throw new RuntimeException("remaining time of job is not 0");
			}
		}
	}
}
